<?php
    include("../lang/cn/company_type_config.php");
	include_once("../includes/page_utf_class.php");
	include_once("$config[webroot]/module/huopin/includes/plugin_huopin_class.php");
	include_once("../module/product/includes/plugin_order_class.php");
	$huopin = new Huopin();
	$order = new order;
	
	if($_POST["act"]=='getSellAmountByCond')
	{//显示销售额
    	$cond = str_replace("\'", "'", $_POST['cond']);
    	$salesInfo = $huopin->getSalesInfo($cond);
    	$html = "<div style='height:200px;'>总金额:".$config['money'].$salesInfo."</div>";
    	die($html);
	}
	
	$sql = "select payment_id,payment_name from ".PAYMENT;
	$db->query($sql);
	$pay = $db->getRows();
	$tpl->assign("pay",$pay);
	
	
	if($_GET['act']=='opear' && $_GET['type'] && $_GET['orderId']>0){//操作订单
	    if($_GET['type']=='pay'){
	        $sql = "select order_id,order_time,total_price from ".HUOPINORDER." where order_id={$_GET['orderId']}";
	        $db->query($sql);
	        $de = $db->fetchRow();
	        $tpl->assign('de',$de);
	    }
	    
	    
	    $tpl->assign('type',$_GET['type']);
	    $tpl->assign('orderId',$_GET['orderId']);
	    $tpl->display('order_opear.htm');
	    exit;
	}
	if($_POST['type'] && $_POST['orderId']>0){
	    switch ($_POST['type'])
	    {
	        case 'cancel':
	            $huopin->cancelHpOrder($_POST['orderId'],$_POST['admin_remark']);break;
	        case 'pay':
	            $payment_name = "";
	            $sql = "select payment_name from ".PAYMENT." where payment_id={$_POST['payment_id']}";
	            $db->query($sql);
	            $payment_name = $db->fetchField('payment_name');
	            $data = array(
	                'payment_id'=>$_POST['payment_id'],
	                'payment_name'=>$payment_name,
	                'admin_remark'=>'后台支付，操作人'.$_SESSION['ADMIN_USER']
	            );
	            
	            $huopin->updateOderStatus($_POST['orderId'], 2, $data);
	            
	            include_once("../module/payment/includes/payment_class.php");
	            $payment = new payment();
	            $payment->apply_result($_POST['orderId']);
	        default:
	            break;
	    }
	    msg("module.php?m=huopin&s=order.php");
	}
	
	if($_GET['operation']=="edit")
	{
		if($_POST['act'])
		{	
			unset($_GET['operation']);
			unset($_GET['s']);
			unset($_GET['m']);
			$time=time();
			
			//修改
			if($_POST["act"]=='shipping' and is_numeric($_POST['id']))
			{
				$sql="update ".HUOPINORDER." set shipping_name='$_POST[shipping_name]',shipping_address='$_POST[t] $_POST[shipping_address]',shipping_tel='$_POST[shipping_tel]',shipping_company='$_POST[shipping_company]',shipping_code='$_POST[shipping_code]',status='40',shipping_time='$time' where id='".$_POST['id']."'";
				$db->query($sql);
				unset($_GET['editid']);
			}
			if($_POST["act"]=='cancel' and is_numeric($_POST['id']))
			{
				$admin_remark=$_POST['other_reason']?$_POST['admin_remark']." ".$_POST['other_reason']:$_POST['admin_remark'];
				
				$sql = "select order_id from ".HUOPINORDER." where id='{$_POST['id']}'";
				$db->query($sql);
				$order_id = $db->fetchField('order_id');
				//取消
				$huopin->cancelHpOrder($order_id,$admin_remark);
				
				unset($_GET['editid']);
			}
			$getstr=implode('&',convert($_GET));
			msg("?m=huopin&s=order.php$getstr");
		}
		//信息
		if($_GET['editid'] and is_numeric($_GET['editid']))
		{
			$sql="select * from ".HUOPINORDER." where id='$_GET[editid]'";
			$db->query($sql);
			$de=$db->fetchRow();
			
			//商品信息
			$productList = array();
			if(!empty($de)){
			    $sql = "select * from ".HUOPINORDERPRO." where order_id={$de['order_id']}";
			    $db->query($sql);
			    $productList = $db->getRows();
			}
			$tpl->assign('productList',$productList);
			
// 			$sql="select * from ".FASTMAIL."  order by id";
// 			$db->query($sql);
// 			$re=$db->getRows();
// 			$tpl->assign("re",$re);
			
			$tpl->assign("district",GetDistrict());
			$tpl->assign("config",$config);
		}
	}
	else
	{
		$str="";
		
		//----------------------分仓
		$general_shop =$order->get_general_shop();
		
		if ($general_shop!=0&&$general_shop!=$_GET['stock_shop']&&isset($_GET['stock_shop'])){
		    msg('noright.php');
		    exit;
		}
		if ($general_shop){
		    $_GET['stock_shop'] =$general_shop;
		}
		if ($_GET['stock_shop']){
		    $str.=" and a.stock_shop = '$_GET[stock_shop]' ";
		}
		//-----------------
		
		if(is_numeric($_GET['type']))
		{
			$str .=" and a.status = '$_GET[type]' ";
		}else{
		    $str .=" and a.status >0";
		}
		if($_GET['code']){
		    $str .= " and a.order_id='{$_GET['code']}'";
		}
		if($_GET['order_pay_status'])
		{
		    $str.=" and a.payment_status = '$_GET[order_pay_status]' ";
		}
		if($_GET['pay'])
		{
		    $str.=" and a.payment_id = '$_GET[pay]' ";
		}
		if($_GET['maxp'])
		{
		    $str.=" and a.total_price <= '$_GET[maxp]' ";
		}
		if($_GET['minp'])
		{
		    $str.=" and a.total_price >= '$_GET[minp]' ";
		}
		if($_GET['stime'])
		{
		    $str.=" and IFNULL(a.order_time,0) > '".date('Y-m-d',strtotime($_GET[stime]))."' ";
		}
		if($_GET['etime'])
		{
		    $str.=" and IFNULL(a.order_time,0) < '".date('Y-m-d',strtotime($_GET[etime]))." 23:59:59' ";
		}
		if($_GET['p_stime'])
		{
		    $str.=" and IFNULL(a.pay_time,0) > '".date('Y-m-d',strtotime($_GET[p_stime]))."' ";
		}
		if($_GET['p_etime'])
		{
		    $str.=" and IFNULL(a.pay_time,0) < '".date('Y-m-d',strtotime($_GET[p_etime]))." 23:59:59' ";
		}
		if($_GET['user'])
		{
		    $sqluser="select userid from ".MEMBER." where  username like '%$_GET[user]%' or name like '%$_GET[user]%' limit 1";
		    $db->query($sqluser);
		    $userid=$db->fetchField('userid');
		    $str.=" and a.member_id='$userid'";
		}
		if($_GET['userid']){
		    $str.=" and a.member_id='$_GET[userid]'";
		}
		if($_GET['hp_status'])
		{
		    $time = date('Y-m-d H:i:s');
		    switch ($_GET['hp_status'])
		    {
		        case 1://伙拼中
		            $str .= " and b.status=1 and b.start_time<'{$time}' and b.end_time>'{$time}' and ((b.true_amount+b.virtual_amount)<b.low_amount)";break;
		        case 2://伙拼中(已成功)
		            $str .= " and b.status=1 and b.start_time<'{$time}' and b.end_time>'{$time}' and ((b.true_amount+b.virtual_amount)>=b.low_amount)";break;
		        case 3://伙拼结束(已成功)
		            $str .= " and b.status=1 and b.end_time<='{$time}' and ((b.true_amount+b.virtual_amount)>=b.low_amount)";break;
		        case 4://伙拼结束(已失败)
		            $str .= " and b.status=1 and b.end_time<='{$time}' and ((b.true_amount+b.virtual_amount)<b.low_amount)";break;
		        case 5://配货中
		            $str .= " and b.status=2 ";break;
		        case 6://已完成
		            $str .= " and b.status=3 ";break;
		        default:
		            break;
		    }
		}
		if($_GET['onlytoday']){
		    $timeType = $_GET['onlytoday'];
		    $str .=" and a.status >1";
		    switch ($timeType)
		    {
		        case 1://今天
		            $time = date('Y-m-d');
		            $str .= " and a.order_time>='{$time}'";
		            break;
		        case 2://昨天
		            $time = date('Y-m-d',strtotime('-1 day'));
		            $rtime = $time." 23:59:59";
		            $str .= " and a.order_time>='{$time}' and a.order_time<='{$rtime}'";
		            break;
		        case 3://本月
		            $time = date('Y-m');
		            $str .= " and a.order_time>='{$time}'";
		            break;
		        default://全部
		            break;
		    }
		}
		
		//查询总金额的条件
		$amountCond = "";
		$amountCond = str_replace("'", "\'", $str);
		$tpl->assign('amountCond',$amountCond);
		
		$sql="select a.*,b.hp_title,b.status as hp_status,b.end_time,b.true_amount,b.virtual_amount,b.low_amount,m.username,m.name as nick_name from ".HUOPINORDER." as a left join ".HUOPIN." as b on a.hp_id=b.id left join ".MEMBER." as m on a.member_id=m.userid where 1 $str order by a.create_time desc ";
		//=============================
		$page = new Page;
		$page->listRows=20;
		if (!$page->__get('totalRows')){
			$db->query($sql);
			$de['count']=$page->totalRows = $db->num_rows();
		}
		$sql .= "  limit ".$page->firstRow.",".$page->listRows;
		$pages = $page->prompt();
		$de['count']=$de['count']?$de['count']:($_GET['totalRows']?$_GET['totalRows']:'0');
		$de['page']=$pages;
		$de['perPageRows'] = $page->listRows;
		//=====================
		$db->query($sql);
		$de['list']=$db->getRows();
		
		foreach ($de['list'] as $k=>$v)
		{
		    $de['list'][$k]['client_platform_str'] = $client_platform[$v['client_platform']];
		    $de['list'][$k]['client_system_str'] = $client_platform[$v['client_system']];
		    
		    $hpStatusStr = "";
		    switch ($v['hp_status'])
		    {
		        case 1:
		            if(strtotime($v['end_time'])>time()){
		                $hpStatusStr = "伙拼中";
		                if((($v['true_amount']+$v['virtual_amount'])>=$v['low_amount'])){
		                    $hpStatusStr .= "(已成功)";
		                }
		            }else{
		                $hpStatusStr = "伙拼结束";
		                if((($v['true_amount']+$v['virtual_amount'])>=$v['low_amount'])){
		                    $hpStatusStr .= "(已成功)";
		                }else{
		                    $hpStatusStr .= "(已失败)";
		                }
		            }
		            break;
		        case 2:
		            $hpStatusStr = "配货中";break;
		        case 3:
		            $hpStatusStr = "已完成";break;
		        default:
		            break;
		    }
		    
		    $de['list'][$k]['hpStatusStr'] = $hpStatusStr;
		}
	}
	
	$statu_list =array('0'=>'已取消','1'=>'未支付','2'=>'已支付');
	
	$stock_shops=$order->get_stock_shops();
	$tpl->assign("stock_shops",$stock_shops);
	
	$tpl->assign("de",$de);
	$tpl->assign("statu_list",$statu_list);
	$tpl->display("hp_order.htm");

?>